package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetIDaompl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    OrdersDao ordersDao = new OrdersDaoImpl();
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;
    //根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录
    //​ 1.并获取自动生成的订单编号，
    //​ 2.然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
    //​ 3.然后根据用户编号、商家编号删除购物车表中的数据。
    @Override
    public int sinsert(Orders orders) {
        //a返回的是主键
        int a = 0;
        try {
            //确保数据库的一致性,开启事务
            DBUtil.beginTransaction();
            CartDao cartDao = new CartDaoImpl();
            Cart cart = new Cart();
            String uid = orders.getUserId();
            Integer bid = orders.getBusinessId();
            cart.setUserId(uid);
            cart.setBusinessId(bid);
            //查询购物车
            List<Cart> list = cartDao.query(cart);
            //创建一条订单记录并返回主键
            a = ordersDao.insert(orders);
            //批量添加明细
            OrderDetailetDao orderDetailetDao = new OrderDetailetIDaompl();
            List<OrderDetailet> li = new ArrayList<>();
            for (Cart c:list) {
                OrderDetailet orderDetailet = new OrderDetailet();
                orderDetailet.setOrderId(a);
                orderDetailet.setFoodId(c.getFoodID());
                orderDetailet.setQuantity(c.getQuantity());
                li.add(orderDetailet);
            }
            //批量添加
            orderDetailetDao.save(li);
            //删除购物车
            for (int i = 0 ;i<li.size();i++){
                Cart cart1 = new Cart();
                cart1.setUserId(uid);
                cart1.setBusinessId(bid);
                if(li.get(i).getFoodId()!=0){
                    cart1.setFoodID(li.get(i).getFoodId());
                }
                //写在循环里批量删除,因为购物车里的方法已经写好了,没有重写.
                cartDao.delete(cart1);
            }
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        //依然返回的是订单编号
        return a;
    }
//根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
    @Override
    public Orders squeryobo(Integer orderId) {
        Orders orders = new Orders();
        try {
            orders = ordersDao.queryobo(orderId);
            OrderDetailetDao orderDetailetDao = new OrderDetailetIDaompl();
            //一对多：订单明细信息
            List<OrderDetailet> list = orderDetailetDao.query(orderId);
            orders.setList(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }
//根据用户编号查询此用户的所有订单信息
    @Override
    public List<Orders> squeryall(String userId) {
        List<Orders> list = new ArrayList<>();
        try {
            list = ordersDao.queryall(userId);
            for (int i = 0;i<list.size();i++){
                int orderid = list.get(i).getOrderId();
                OrderDetailetDao orderDetailetDao = new OrderDetailetIDaompl();
                //一对多：订单明细信息
                List<OrderDetailet> l = orderDetailetDao.query(orderid);
                list.get(i).setList(l);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }
//根据orderid修改支付状态
    @Override
    public int pay(Integer orderId) {
        int a = 0;
        try {
            DBUtil.beginTransaction();
            a = ordersDao.pay(orderId);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }finally {
            DBUtil.close();
        }
        return a;
    }
}



